package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 *
 * @author alexis
 * @date 2025-01-15
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return alertDataMapper.deleteById(id);
    }

    @Override
    public void handleAlertData(AlertData alertData) {
        // 拿到当前登录的用户Id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = null;
        if (authentication != null) {
            loginUser = (LoginUser) authentication.getPrincipal();
        }
        SysUser sysUser = userService.selectUserById(loginUser.getUserId());


        AlertData one = getOne(new LambdaQueryWrapper<AlertData>().eq(AlertData::getId, alertData.getId()));
        boolean updateRes = update(new LambdaUpdateWrapper<AlertData>().eq(AlertData::getProductKey,
                one.getProductKey()).eq(AlertData::getCreateTime, one.getCreateTime()).set(AlertData::getStatus, 1).set(AlertData::getProcessingTime, alertData.getProcessingTime()).set(AlertData::getProcessingResult, alertData.getProcessingResult()).set(AlertData::getProcessorId, sysUser.getUserId()).set(AlertData::getProcessorName, sysUser.getNickName()));
        if (!updateRes) {
            throw new BaseException("修改失败");
        }
    }

    @Override
    public List<AlertData> selectAlertDataListByPage(String deviceName, LocalDateTime startTime,
                                                     LocalDateTime endTime, Integer status) {
        // 拿到当前登录的用户Id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = null;
        if (authentication != null) {
            loginUser = (LoginUser) authentication.getPrincipal();
        }
        Long userId = loginUser.getUserId();

        //根据报警设备查询相关负责人id
        return list(new LambdaQueryWrapper<AlertData>()
                .eq(AlertData::getUserId, userId)
                .eq(status != null, AlertData::getStatus, status)
                .eq(deviceName != null, AlertData::getDeviceName, deviceName)
                .gt(startTime != null, AlertData::getCreateTime, startTime)
                .lt(endTime != null, AlertData::getCreateTime, endTime));
    }
}
